//
//  AddOtherFeeDetailVC.m
//  NtoccShipper
//
//  Created by mac on 2019/12/23.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AddOtherFeeDetailVC.h"

@interface AddOtherFeeDetailVC ()
@property (nonatomic,strong) CJTextField *fieldFeeType;
@property (nonatomic,copy) NSString *strFeeName;
@property (nonatomic,copy) NSString *strFeeCode;
@property (nonatomic,strong) CJTextField *fieldFeeNum; // 费用
@property (nonatomic,strong) CJTextField *fieldPayType;//收支类型：0-收入（司机出） ，1-支出（货主出）
@property (nonatomic,copy) NSString *strPayType;
@property (nonatomic,strong) CJTextField *fieldRemark;
@end

@implementation AddOtherFeeDetailVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    if (self.isEditor) {
        [self setTitleName:CJLString(@"编辑其他费用")];
    }else{
        [self setTitleName:CJLString(@"添加其他费用")];
    }
    [self setBackLeftButton:@""];
    [self createTableView];

    self.strPayType = @"";
    self.strFeeCode = @"";
    self.strFeeName = @"";
    
    if (self.isEditor) {
        [self loadDataRefreshView];
    }
    [self setFieldLimit];
}

- (void)setFieldLimit{
    self.fieldFeeNum.textFieldType = CJTextFieldStringTypeMoneyValue;
    self.fieldFeeNum.maxLength = CJTextLengthTen;
    
}

- (void)loadDataRefreshView{
//    @"orderCode":self.strCode,
//    @"":self.strFeeCode,
//    @"":self.strFeeName,
//    @"":self.fieldFeeNum.text,
//    @"":self.strPayType,
//    @"extraFeeRemark":self.fieldRemark.text
//    DLog(@"===== %@",self.dicData);
    self.strCode = CJStringWithFormat(@"%@",self.dicData[@"orderCode"]);
    self.strFeeCode = CJStringWithFormat(@"%@",self.dicData[@"extraFeeType"]);
    self.fieldFeeType.text = CJStringWithFormat(@"%@",self.dicData[@"extraFeeName"]);
    self.fieldFeeNum.text = CJStringWithFormat(@"%@",self.dicData[@"extraFeeAmount"]);
    self.strPayType = CJStringWithFormat(@"%@",self.dicData[@"extraPayType"]);
    self.strFeeName = CJStringWithFormat(@"%@",self.dicData[@"extraFeeName"]);
    if ([self.dicData[@"extraPayType"] intValue] == 0) {
        self.fieldPayType.text = @"收入";
    }else{
        self.fieldPayType.text = @"支出";
    }
    self.fieldRemark.text = CJStringWithFormat(@"%@",self.dicData[@"extraFeeRemark"]);
}
/// 创建tableview 等
- (void)createTableView{
    
    //    WS(ws);
    CGRect rcFrame = CGRectMake(0, kTopHeight, [[UIScreen mainScreen] bounds].size.width, 0);
    FlexFrameView *header = [[FlexFrameView alloc] initWithFlex:@"AddOtherFeeDetailTopView" Frame:rcFrame Owner:self];
    header.flexibleHeight = YES;
    
    [header layoutIfNeeded];
    [self.view addSubview:header];
}

- (void)btnSelectFeeTypeAction{
    [self.view endEditing:NO];
    WS(ws);
    [CJNetWork getSenGoodsDicsTypeinfo:CJExtraFeeType Success:^(id  _Nullable responseObject) {
//        DLog(@"====== %@",responseObject);
        NSDictionary *dic = responseObject[0];
        NSMutableArray *arrMutar = [[NSMutableArray alloc]init];
        NSMutableArray *arrMutarCodes = [[NSMutableArray alloc]init];

        [dic[@"dictList"] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
            [arrMutar addObject:obj[@"name"]];
            [arrMutarCodes addObject:obj[@"code"]];
        }];

        [ws list:arrMutar listCodes:arrMutarCodes title:dic[@"name"]];
    }];
}

- (void)list:(NSArray *)list listCodes:(NSMutableArray *)arryCodes title:(NSString *)strTitle{
    
    [self.view endEditing:NO];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
    stringPickerView.title = strTitle;
    stringPickerView.dataSourceArr = list;
    WS(ws);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        ws.strFeeCode = arryCodes[resultModel.index];
        ws.strFeeName = resultModel.selectValue;
        ws.fieldFeeType.text = resultModel.selectValue;
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}

- (void)btnSelelctPayTypeAction{
    
    [self.view endEditing:NO];
    BRStringPickerView *stringPickerView = [[BRStringPickerView alloc]initWithPickerMode:BRStringPickerComponentSingle];
    stringPickerView.title = @"支出类型";
    stringPickerView.dataSourceArr = @[@"收入",@"支出"];
    WS(ws);
    stringPickerView.resultModelBlock = ^(BRResultModel *resultModel) {
        ws.fieldPayType.text = resultModel.selectValue;
        
        if (resultModel.index == 0) {
            ws.strPayType = @"0";
        }else if (resultModel.index == 1){
            ws.strPayType = @"1";
        }
    };
    
    BRPickerStyle *customStyle = [BRPickerStyle pickerStyleWithThemeColor:[UIColor cjMainColor]];
    stringPickerView.pickerStyle = customStyle;
    [stringPickerView show];
}

- (void)btnSaveAction{
    
    if ([CJRegular getIsStrEmpty:self.strFeeCode]) {
        [CJAlert showStringDismissWithDelay:@"请选择费用类型"];
    }else if([CJRegular getIsStrEmpty:self.fieldFeeNum.text]) {
        [CJAlert showStringDismissWithDelay:@"请填写费用"];
    }else if ([CJRegular getIsStrEmpty:self.strPayType]){
        [CJAlert showStringDismissWithDelay:@"请选择支出类型"];
    }else{
        
        NSDictionary *dic = @{
            @"orderCode":self.strCode,
            @"extraFeeType":self.strFeeCode,
            @"extraFeeName":self.strFeeName,
            @"extraFeeAmount":self.fieldFeeNum.text,
            @"extraPayType":self.strPayType,
            @"extraFeeRemark":self.fieldRemark.text
        };
        
        if (self.isEditor) {
            if (self.blockEditor) {
                self.blockEditor(self.strIndex, dic);
                [self pop];
            }
        }else{
            if (self.blockSuccess) {
                self.blockSuccess(dic);
                [self pop];
            }
        }
    }
}

- (void)btnCancleAction{
    
//    if (self.blockSuccess) {
//        self.blockSuccess(@"");
        [self pop];
//    }
}


@end
